package com.hxzy.controller.anonymous.dictdata;

import com.hxzy.common.enums.AckCode;
import com.hxzy.common.resp.R;
import com.hxzy.common.util.DictDataCacheUtil;
import com.hxzy.controller.vue.dictdata.resp.SysDictDataResp;
import com.hxzy.service.SysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/9/4 9:14
 * @Version 1.0
 */
@RequestMapping(value = "/anon/dictdata")
@RestController
@Api(tags = "公共API-数据字典")
public class AnonyDictDataController {


    /**
     * 数据字典缓存
     */
    @Autowired
    private DictDataCacheUtil  dictDataCacheUtil;

    @ApiOperation(value = "根据dictType去缓存中查询值")
    @GetMapping(value = "/bytype/{dictType}")
    public R  findByDictType(@PathVariable(value = "dictType") String dictType){
        List<SysDictDataResp> byDictType = this.dictDataCacheUtil.findByDictType(dictType);
        if(byDictType.isEmpty()){
            R.build(AckCode.NOT_FOUND_DATA);
        }

        // label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass
        List<Map> collect = byDictType.stream().map(p -> {
            Map<String,Object> map = new HashMap(16);
            map.put("label", p.getDictLabel());
            map.put("value", p.getDictValue());
            map.put("elTagType", p.getListClass());
            map.put("elTagClass", p.getCssClass());
            return map;
        }).collect(Collectors.toList());

        return R.okHasData(collect);
    }

}
